package com.team7.happycommunity.propertymanagement.controller;

import com.github.pagehelper.PageInfo;
import com.team7.happycommunity.propertymanagement.common.CommonResult;
import com.team7.happycommunity.propertymanagement.dto.PropertyserviceForSave;
//import com.team7.happycommunity.propertymanagement.service.PropertyService;
import com.team7.happycommunity.propertymanagement.pojo.Announcement;
import com.team7.happycommunity.propertymanagement.pojo.PpPayservice;
import com.team7.happycommunity.propertymanagement.service.PropertyServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/propertyservice")
public class PropertyServiceController {

    @Autowired
    PropertyServiceService propertyServiceService;

    /**
     * 新增 + 修改
     * @param propertyserviceForSave
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public CommonResult savePropertyservice(@RequestBody PropertyserviceForSave propertyserviceForSave){
//        System.out.println(propertyserviceForSave.toString());
        boolean result = propertyServiceService.save(propertyserviceForSave);
        return result?CommonResult.success():CommonResult.failed();
    }

    /**
     * 分页获取
     * @param currentPage
     * @param pageSize
     * @param username
     * @return
     */

    @GetMapping("/list")
    @ResponseBody
    public CommonResult getPropertyServiceList(@RequestParam(value="page",defaultValue = "1",required = false) Integer currentPage,
                                               @RequestParam(value="limit",defaultValue = "5",required = false) Integer pageSize,
                                               @RequestParam(value = "username",defaultValue = "null",required = false) String username){


        PageInfo info = null;
        try{
            List<PpPayservice> list_pay = propertyServiceService.getListByPageAndUsername(currentPage,pageSize,username);
            info = new PageInfo(list_pay);
            return CommonResult.success(info);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed();
        }
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @ResponseBody
    public CommonResult delete(Integer id){
        System.out.println(id);
        int result = propertyServiceService.delete(id);
        return (result==0)?CommonResult.failed():CommonResult.success();
    }

}
